//
//  AppDelegate.m
//  QQ空间
//
//  Created by zhangping on 16/1/15.
//  Copyright © 2016年 zhangping. All rights reserved.
//

#import "AppDelegate.h"
#import "CZSplitViewController.h"
#import "CZTabBarController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

/// 程序启动完成
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 1.创建 window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 2.设置背景色
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 显示状态栏
    application.statusBarHidden = NO;
    
    // 3.判断运行的设备
    if (isIPhone) {
        // 运行的设备是 iPhone,设置iPhone主界面
        self.window.rootViewController = [[CZTabBarController alloc] init];
    } else {
        application.statusBarStyle = UIStatusBarStyleLightContent;
        
        // 运行的设备是 iPad,设置iPad主界面
        self.window.rootViewController = [[CZSplitViewController alloc] init];
    }
    
    // 4.让window称为主窗口并显示
    [self.window makeKeyAndVisible];
    
    return YES;
}

/// 设置APP支持的方向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    // iPhone只支持竖屏
    if (isIPhone) {
        return UIInterfaceOrientationMaskPortrait;
    } else {
        // iPad支持所有方向
        return UIInterfaceOrientationMaskAll;
    }
}

/**
 *  屏幕将要旋转
 *  @param newStatusBarOrientation 旋转后的屏幕方向
 *  @param duration                动画时间
 */
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration {
    
    // 1. 判断当前window的根控制器是否是CZSplitViewController
    if (![self.window.rootViewController isKindOfClass:[UISplitViewController class]]) {
        return;
    }
    
    // 2. 获取到CZSplitViewController
    CZSplitViewController * splitVC = (CZSplitViewController *)self.window.rootViewController;
    
    // 3. 设置 master 的宽度
    [splitVC setPrimaryColumnWidthWithOrientation:newStatusBarOrientation];
}

@end
